<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <!-- 条件栏 -->
    <page-table
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :pager="false"
      @handleEvent="handleEvent"
    >
    </page-table>
  </page-dialog>
</template>
<script setup name="AddPurchOrder">
import PageDialog from '@/components/PageDialog/index'
import PageFilter from '@/components/PageFilter/index'
import PageTable from '@/components/PageTable/index'
import { pagePendDeliverProducts } from '@/api/supplier.js'

const { proxy } = getCurrentInstance()
const emit = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

// 过滤相关配置
const filterInfo = reactive({
  query: {
    sort: 'order_no desc',
  },
  list: [
    {
      componentType: 'input',
      label: '采购单',
      value: 'purchOrderNoLike',
      clearable: true,
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = reactive({
  data: [],
  fieldList: [
    { label: '采购单号', value: 'orderNo' },
    { label: '采购部门', value: 'deptName' },
    { label: '状态', value: 'statusValue' },
    // { label: "采购总数", value: "qty", type: "column", formatter: proxy.$fn.qtyFormatter}
  ],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '采购单导入',
  width: '50%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  search()
})

function search() {
  pagePendDeliverProducts(proxy.$fn.delEmptyAttribute(filterInfo.query)).then(
    (res) => {
      if (res && res.success) {
        tableInfo.data = res.data
      }
    }
  )
}
function save() {
  if (selectRow.value) {
    handleClose()
    emit('callback', selectRow.value)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择采购单',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        dialogInfo.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
</script>
<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
